package view.chart;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;

/**
 *
 * @author Dennis Nijssen
 */
public class GraphChart extends Chart {
    private final int padding;
    
    public GraphChart(ChartValue[] values, int padding) {
        super(values);
        
        this.padding = padding;
    }

    public GraphChart(int padding) {
        this(null, padding);
    }

    @Override
    protected void paintChart(Graphics2D g2d) {
        ChartValue[] values = getValues();
                
        if(values == null || values.length == 0) 
            return;
        
        int w = getWidth();
        int h = getHeight();
        
        // Draw vertical line
        g2d.drawLine(padding, padding, padding, (h - padding));
        
        // Draw horizontal line
        g2d.drawLine(padding, (h - padding), (w - padding), (h - padding));
        
        // Get highest value
        int highest = 0;
        for(int i = 0; i < values.length; i++) {
            if(values[i].getValue() > highest) 
                highest = values[i].getValue();
        }
        
        // Calculating size from a step of x
        double stepX = (double)(w - (2 * padding)) / (values.length - 1);
        
        // Get value of 1 pixel
        double pixel = (double)highest / (h - (2 * padding));
                
        // Draw lines of graph
        g2d.setColor(Color.green);
        for(int i = 0; i < values.length - 1; i++) {
            double x1 = padding + (i * stepX);
            double y1 = h - padding - (values[i].getValue() / pixel);
            double x2 = padding + ((i + 1) * stepX);
            double y2 = h - padding - (values[i + 1].getValue() / pixel);
            g2d.draw(new Line2D.Double(x1, y1, x2, y2));
        }
        
        // Mark points
        g2d.setColor(Color.red);
        for(int i = 0; i < values.length; i++) {
            double x = padding + (i * stepX);
            double y = h - padding - (values[i].getValue() / pixel);
            g2d.fill(new Ellipse2D.Double((x - 2), (y - 2), 4, 4));
        }
    }
}
